﻿using Kaiser.Models;
using Kaiser.Modules.Cubic.DomainModel.Board;
using Kaiser.Modules.Cubic.DomainModel.News;
using Kaiser.Modules.Cubic.DomainModel.Product;
using Kaiser.Modules.Cubic.Repositories;
using Kaiser.Modules.Cubic.ViewModel;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Kaiser.Repositories.iBatis.CUBIC
{
    public class ProductRepositorie : IProductRepositorie
    {
        private IBatisHepler IBatisHepler = null;
        public ProductRepositorie()
        {
            IBatisHepler = new BatisHepler("SQLMap.CUBIC.config");
        }

        public DataGrid<Kaiser.Modules.Cubic.DomainModel.Product.ProductDetail> SearchProduct(ProductSearch search, PagingInfo pageinfo)
        {
            var result = new DataGrid<ProductDetail>();

            int totalCount = 0;

            result.TableResult = IBatisHepler.ExcuteSelect("SearchProduct", search, pageinfo.Size, pageinfo.Index + 1, out totalCount);

            result.total = totalCount;

            return result;

        }

        public ProductDetail GetProduct(string id)
        {
            ProductDetail result = IBatisHepler.ExcuteSelectObj("GetProduct", id) as ProductDetail;
            if (result != null)
            {
                var photos =
                    IBatisHepler.ExcuteSelectForList<Photo>("GetPhotoByObjectIDOnCubicPhoto", id).ToList<Photo>();
                result.Photos = photos.Where(x => string.IsNullOrWhiteSpace(x.Type) ).ToList();
                result.Files = IBatisHepler.ExcuteSelectForList<File>("GetPhotoByObjectIDOnCubicFile", id).ToList<File>();
                result.TopPic = photos.FirstOrDefault(x => x.Type == "Top");
            }
            return result;
        }

        /// <summary>
        /// 返回产品的列表通过分类
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public IList<ProductDetail> GetProductsByCategoryCode(string categoryId)
        {
            var result = IBatisHepler.ExcuteSelectForList<ProductDetail>("GetProductsByCategoryCode", categoryId);
            return result;
        }

        /// <summary>
        /// 返回所有的产品分类(当ParentId为空时)
        /// </summary>
        /// <returns></returns>
        public IList<ProductCategory> GetProductCateogrys(string categoryParentId)
        {
            var result = IBatisHepler.ExcuteSelectForList<ProductCategory>("GetProductCateogrys", categoryParentId);
            return result;
        }

        /// <summary>
        /// 返回产品分类的具体内容
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>

        public ProductCategory GetProductCategory(string categoryId)
        {
            ProductCategory result = IBatisHepler.ExcuteSelectObj("GetProductCategory", categoryId) as ProductCategory;
            return result;
        }



        /// <summary>
        /// 返回新闻列表通过新闻分类
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>

        public IList<NewsDetail> GetNewsByCategoryCode(string categoryId)
        {
            var result = IBatisHepler.ExcuteSelectForList<NewsDetail>("GetNewsByCategoryCode", categoryId);
            return result;
        }


        public string AddMessage(Message msg)
        {
            var result = IBatisHepler.ExcuteInsert("AddMessage", msg);
            return "";
        }

        /// <summary>
        /// 返回新闻详细内容
        /// </summary>
        /// <param name="newsId"></param>
        /// <returns></returns>
        public NewsDetail GetNews(string newsId)
        {
            NewsDetail result = IBatisHepler.ExcuteSelectObj("GetNews", newsId) as NewsDetail;
            return result;
        }

        public void Insert(ProductDetail model)
        {
            var result = IBatisHepler.ExcuteInsert("InsertProduct", model);
        }

        public void Update(ProductDetail model)
        {
            var result = IBatisHepler.ExcuteUpdate("UpdateProduct", model);
        }

        public int Delete(object primaryId)
        {
            var parms = new Hashtable();
            parms["ids"] = primaryId.ToString().Split(',');
            var result = IBatisHepler.ExcuteDelete("DeleteProduct", primaryId);
            return result;
        }

        public ProductDetail Get(object id)
        {
            ProductDetail result = IBatisHepler.ExcuteSelectObj("GetProduct", id) as ProductDetail;
            if (result != null)
            {
                result.Photos = IBatisHepler.ExcuteSelectForList<Photo>("GetPhotoByObjectIDOnCubicPhoto", id).ToList<Photo>();
                result.Files = IBatisHepler.ExcuteSelectForList<File>("GetPhotoByObjectIDOnCubicFile", id).ToList<File>();
            }
            return result;
        }


        public int DeleteList(IList<string> ids)
        {
            throw new System.NotImplementedException();
        }





        public ProductCategory GetProductCategoryByCode(string categoryCode)
        {
            var result = IBatisHepler.ExcuteSelectObj("GetProductCategoryByCode", categoryCode) as ProductCategory;
    
            return result;
        }


        public DataGrid<Photo> SearchProductPhoto(string productId,PagingInfo pageinfo)
        {
            var result = new DataGrid<Photo>();

            int totalCount = 0;

            result.TableResult = IBatisHepler.ExcuteSelect("SearchProductPhoto", productId, pageinfo.Size, pageinfo.Index + 1, out totalCount);

            result.total = totalCount;

            return result;
        }


        public void InsertProductPhoto(Photo model)
        {
            var result = IBatisHepler.ExcuteInsert("ProductPhoto.Insert", model);
        }

        public void UpdateProductPhoto(Photo model)
        {
            var result = IBatisHepler.ExcuteUpdate("ProductPhoto.Update", model);
        }

        public void DelProductPhoto(string id)
        {
            var result = IBatisHepler.ExcuteDelete("ProductPhoto.DeleteList", id);
        }

        public Photo GetProductPhoto(string id)
        {
            var result = IBatisHepler.ExcuteSelectObj("ProductPhoto.Get", id)   as Photo;
            return result;
        }




 
    }
}
